package app.web.admin.controller;

import app.config.Config;
import app.web.admin.domain.ao.RoleCreateAo;
import app.web.admin.domain.ao.RolePatchByIdAo;
import app.web.admin.domain.pojo.AdminRole;
import app.web.admin.service.RoleService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.gitee.feizns.quickstart.domain.page.PageRecordsVo;
import com.gitee.feizns.quickstart.domain.vo.Ret;
import com.gitee.feizns.quickstart.web.domain.query.QueryParamsAo;
import com.gitee.feizns.quickstart.web.restful.Rests;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * 管理模块/角色
 * @author feizns
 * @since 2025-04-09
 */
@Validated
@SaCheckLogin
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/admin/role")
public class RoleController {

    /**
     * 角色服务
     */
    private final RoleService service;

    /**
     * 分页
     * @param query 查询参数
     * @return {@link Ret}<{@link PageRecordsVo}<{@link AdminRole}>>
     */
    @GetMapping
    public Ret<PageRecordsVo<AdminRole>> page(QueryParamsAo query) {
        return Ret.ok(Rests.page(AdminRole.class, query.page(), query));
    }

    /**
     * 角色组列表
     * @return {@link Ret }<{@link Set }<{@link String }>>
     */
    @GetMapping("/groups")
    public Ret<Set<String>> groups() {
        return Ret.ok(Config.roleGroupNames());
    }

    /**
     * 新增
     * @param ao 请求参数
     * @return {@link Ret}<{@link AdminRole}>>
     */
    @PostMapping
    public Ret<AdminRole> create(@Validated @RequestBody RoleCreateAo ao) {
        return Ret.ok(service.create(ao));
    }

    /**
     * 详情
     * @param id 编号
     * @return {@link Ret}<{@link AdminRole}>
     */
    @GetMapping("/{id}")
    public Ret<AdminRole> getById(@PathVariable Long id) {
        return Ret.ok(Rests.getById(AdminRole.class, id));
    }

    /**
     * 修改
     * @param id 编号
     * @param ao 请求参数
     * @return {@link Ret}<{@link AdminRole >
     */
    @PatchMapping("/{id}")
    public Ret<AdminRole> patchById(@PathVariable Long id, @Validated @RequestBody RolePatchByIdAo ao) {
        return Ret.ok(service.patchById(id, ao));
    }

    /**
     * 删除
     * @param id 编号
     * @return {@link Ret}<{@link AdminRole}>
     */
    @DeleteMapping("/{id}")
    public Ret<AdminRole> deleteById(@PathVariable Long id) {
        return Ret.ok(service.deleteById(id));
    }

}
